<template>
  <div class="app-container">
    <!-- 添加装饰性GIF -->
    <div class="decorative-gif">
      <img src="http://tc.mcshare.cn/LightPicture/2025/01/ddeae934ba0480f8.gif" alt="decorative">
    </div>

    <!-- 顶部统计卡片 -->
    <el-row :gutter="20" class="stat-cards">
      <el-col :xs="24" :sm="12" :md="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-content">
            <div class="stat-icon">
              <i class="el-icon-s-platform"></i>
            </div>
            <div class="stat-info">
              <div class="stat-label">总服务器</div>
              <div class="stat-value">{{ total }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="12" :md="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-content">
            <div class="stat-icon running">
              <i class="el-icon-video-play"></i>
            </div>
            <div class="stat-info">
              <div class="stat-label">运行中</div>
              <div class="stat-value">{{ total }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="12" :md="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-content">
            <div class="stat-icon tasks">
              <i class="el-icon-s-data"></i>
            </div>
            <div class="stat-info">
              <div class="stat-label">总任务数</div>
              <div class="stat-value">{{ totalTasks }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="12" :md="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-content">
            <div class="stat-icon users">
              <i class="el-icon-user"></i>
            </div>
            <div class="stat-info">
              <div class="stat-label">创建者</div>
              <div class="stat-value">{{ uniqueCreators }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 搜索区域 -->
    <el-card class="search-card" shadow="hover">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <el-form-item label="服务器" prop="serverName">
          <el-input
            v-model="queryParams.serverName"
            placeholder="请输入服务器名称"
            clearable
            prefix-icon="el-icon-search"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:info:add']"
          >创建服务器</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </el-card>

    <!-- 表格区域 -->
    <el-card class="table-card" shadow="hover">
      <el-table
        v-loading="loading"
        :data="infoList"
        @selection-change="handleSelectionChange"
        border
      >
        <el-table-column type="selection" width="45" align="center" />
        <el-table-column label="服务器编号(SID)" align="center" prop="sid" width="130" />
        <el-table-column label="Server-Key" align="center" prop="serverKey" width="300" />
        <el-table-column label="服务器名称" align="center" prop="serverName" width="300">
          <template slot-scope="scope">
            <el-tag size="medium">{{ scope.row.serverName }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="任务数量" align="center" prop="taskCount" width="100">
          <template slot-scope="scope">
            <el-badge :value="scope.row.taskCount" :max="99" type="primary"/>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <el-tag type="success">运行中</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建人" align="center" prop="createName" width="120" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="320">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:info:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-s-tools"
              @click="handleConfigTask(scope.row)"
              v-hasPermi="['system:info:config']"
            >配置任务</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              class="delete-btn"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:info:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加/修改对话框 -->
    <el-dialog 
      :title="title" 
      :visible.sync="open" 
      width="500px" 
      append-to-body
      :close-on-click-modal="false"
      class="custom-dialog"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="服务器名称" prop="serverName">
          <el-input v-model="form.serverName" placeholder="请输入服务器名称" />
        </el-form-item>
        
        
        <el-form-item label="服务器状态" prop="isDel">
          <el-switch
            v-model="form.isDel"
            active-text="启用"
            inactive-text="停用"
            :active-value="'true'"
            :inactive-value="'false'"
          />
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <page-footer />
  </div>
</template>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  height: 100%;
  position: relative;

  // 修改装饰性GIF的样式
  .decorative-gif {
    position: fixed;
    right: 40px;
    bottom: 100px;
    z-index: 10;

    img {
      width: 200px;
      height: auto;
      opacity: 1;
      border-radius: 0;
      box-shadow: none;
      animation: float 3s ease-in-out infinite;
      background: transparent;
      border: none;
    }

    // 确保容器也没有任何装饰
    background: transparent;
    border: none;
    box-shadow: none;
    padding: 0;
    margin: 0;
  }

  // 添加漂浮动画
  @keyframes float {
    0% {
      transform: translateY(0px);
    }
    50% {
      transform: translateY(-10px);
    }
    100% {
      transform: translateY(0px);
    }
  }

  .stat-cards {
    margin-bottom: 20px;

    .stat-card {
      border-radius: 8px;
      transition: all 0.3s;

      &:hover {
        transform: translateY(-5px);
        box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
      }

      .stat-content {
        display: flex;
        align-items: center;
        padding: 10px;

        .stat-icon {
          width: 48px;
          height: 48px;
          border-radius: 12px;
          display: flex;
          align-items: center;
          justify-content: center;
          margin-right: 15px;
          background: #409EFF;
          color: white;

          i {
            font-size: 24px;
          }

          &.running {
            background: #67C23A;
          }

          &.tasks {
            background: #E6A23C;
          }

          &.users {
            background: #F56C6C;
          }
        }

        .stat-info {
          .stat-label {
            font-size: 14px;
            color: #606266;
            margin-bottom: 5px;
          }

          .stat-value {
            font-size: 24px;
            font-weight: bold;
            color: #303133;
          }
        }
      }
    }
  }

  .search-card, .table-card {
    border-radius: 8px;
    margin-bottom: 20px;
  }

  .el-table {
    border-radius: 8px;
    overflow: hidden;

    .delete-btn {
      color: #F56C6C;
    }

    .el-button {
      margin: 0 5px;
      
      &[type="text"] {
        padding: 0;
        
        &:hover {
          color: #409EFF;
          
          &.delete-btn {
            color: #F56C6C;
          }
        }
      }
    }
  }

  .custom-dialog {
    .el-dialog__body {
      padding: 20px 40px;
    }

    .el-form-item {
      margin-bottom: 20px;
    }
  }

  // 表格样式优化
  ::v-deep .el-table {
    background-color: transparent !important;
    
    &::before,
    &::after {
      display: none !important;
    }
    
    // 表头样式
    th.el-table__cell {
      background-color: rgba(255, 255, 255, 0.15) !important;
      border-bottom: 1px solid rgba(255, 255, 255, 0.1) !important;
      color: #333;
      font-weight: bold;
    }
    
    // 单元格样式
    td.el-table__cell {
      background-color: rgba(255, 255, 255, 0.08) !important;
      border-bottom: 1px solid rgba(255, 255, 255, 0.05) !important;
    }
    
    // 鼠标悬停效果
    tr:hover > td.el-table__cell {
      background-color: rgba(255, 255, 255, 0.15) !important;
    }

    // 表格内的标签样式
    .el-tag {
      background-color: rgba(255, 255, 255, 0.15);
      border: 1px solid rgba(255, 255, 255, 0.2);
    }

    // 去除表格边框
    .el-table__fixed-right::before,
    .el-table__fixed::before {
      display: none !important;
    }
  }

  // 分页器样式
  ::v-deep .el-pagination {
    background-color: transparent;
    padding: 15px 0;
    
    .btn-prev,
    .btn-next,
    .el-pager li {
      background-color: rgba(255, 255, 255, 0.2);
      color: #333;
      
      &:hover {
        background-color: rgba(255, 255, 255, 0.3);
      }
      
      &.active {
        background-color: rgba(64, 158, 255, 0.8);
        color: #fff;
      }
    }
  }
}
</style>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/system/info";
import PageFooter from '@/components/Footer'

export default {
  name: "Info",
  components: {
    PageFooter
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务器信息主表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serverKey: null,
        serverName: null,
        taskCount: null,
        isDel: null,
        createName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        serverKey: [
          { required: true, message: "服务器秘钥不能为空", trigger: "blur" }
        ],
        serverName: [
          { required: true, message: "服务器名称不能为空", trigger: "blur" }
        ],
        taskCount: [
          { required: true, message: "总任务数量不能为空", trigger: "blur" }
        ],
        isDel: [
          { required: true, message: "是否删除服务器不能为空", trigger: "blur" }
        ],
        createName: [
          { required: true, message: "服务器创建人不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  computed: {
    totalTasks() {
      return this.infoList.reduce((sum, item) => sum + (Number(item.taskCount) || 0), 0);
    },
    uniqueCreators() {
      return new Set(this.infoList.map(item => item.createName)).size;
    },
    getRunningCount() {
      return this.infoList.filter(item => item.status === 1 || item.status === '1' || item.status === '运行中').length
    }
  },
  created() {
    this.getList();
  },
  methods: {
    // 添加时间格式化方法
    formatDateTime(date) {
      const pad = (num) => String(num).padStart(2, '0');
      const d = date || new Date();
      return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
    },

    /** 查询服务器信息主列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sid: null,
        serverKey: null,
        serverName: null,
        taskCount: null,
        isDel: null,
        createName: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加服务器信息主";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sid = row.sid || this.ids
      getInfo(sid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改服务器信息主";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.createTime = this.formatDateTime(new Date());

          if (this.form.sid != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sids = row.sid || this.ids;
      this.$modal.confirm('是否确认删除服务器信息主编号为"' + sids + '"的数据项？').then(function() {
        return delInfo(sids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    /** 配置任务按钮操作 */
    handleConfigTask(row) {
      // 跳转到B站任务配置页面，并传递服务器信息
      this.$router.push({
        path: '/server/bilibili',
        query: {
          sid: row.sid,
          serverKey: row.serverKey
        }
      });
    }
  }
};
</script>
